package com.aerozhonghuan.logisticsmall.modules.home.search;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.aerozhonghuan.foundation.base.BaseFragment;
import com.aerozhonghuan.logisticsmall.R;
import com.aerozhonghuan.logisticsmall.framework.DB.DBAdapter;
import com.aerozhonghuan.logisticsmall.modules.common.entity.SearchProductHistory;

import java.util.ArrayList;
import java.util.Collections;

/**
 * Created by ChenLin on 2019/5/22 9:31.Email&WeChat:7877409@163.com
 */
public class SoonSearchFragment extends BaseFragment implements View.OnClickListener, AdapterView.OnItemClickListener {

    private MKeyWordSearchActivity mActivity;
    public ListView mSearchHistoryListView;
    private SoonAdapter mAdapter;
    public DBAdapter mDbAdapter;
    private Button mClearBtn;
    private TextView mShowSearchView;

    @Override
    public void onAttach(Activity activity) {
        this.mActivity = (MKeyWordSearchActivity) activity;
        super.onAttach(activity);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.clear_search_history:
                for (SearchProductHistory product : mActivity.mSearchProductHistories) {
                    long id = product.getTimeId();
                    mDbAdapter.open().deleteSearchProductData(String.valueOf(id));
                }
                mActivity.mSearchProductHistories.clear();
                mAdapter.notifyDataSetChanged();
            /*if (mActivity.mSearchProductHistories.size() == 0) {
                mClearBtn.setVisibility(View.GONE);
                mShowSearchView.setVisibility(View.VISIBLE);
            }else {
                mClearBtn.setVisibility(View.VISIBLE);
                mShowSearchView.setVisibility(View.GONE);
            }*/
                mClearBtn.setVisibility(View.GONE);
                mShowSearchView.setVisibility(View.VISIBLE);
                break;

            default:
                break;
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.soon_search_fragment, null);
        mSearchHistoryListView = (ListView) view.findViewById(R.id.soon_search);
        mClearBtn = (Button) view.findViewById(R.id.clear_search_history);
        mClearBtn.setOnClickListener(this);
        mShowSearchView = (TextView) view.findViewById(R.id.show_search_text);
        mDbAdapter = new DBAdapter(mActivity);
        Collections.reverse(mActivity.mSearchProductHistories);
        mAdapter = new SoonAdapter(mActivity.mSearchProductHistories, mActivity);
        mSearchHistoryListView.setAdapter(mAdapter);

        if (mActivity.mSearchProductHistories.size() == 0) {
            mClearBtn.setVisibility(View.GONE);
            mShowSearchView.setVisibility(View.VISIBLE);
        } else {
            mClearBtn.setVisibility(View.VISIBLE);
            mShowSearchView.setVisibility(View.GONE);
        }
        mSearchHistoryListView.setOnItemClickListener(this);
        return view;
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
        SearchProductHistory product = mActivity.mSearchProductHistories.get(position);
        String text = product.getProcutName();
        mActivity.mSearchText.setText(text);
        mActivity.mSearchBtn.performClick();
    }

    class SoonAdapter extends BaseAdapter {
        ArrayList<SearchProductHistory> list;
        Context context;

        public SoonAdapter(ArrayList<SearchProductHistory> lists, Context c) {
            list = lists;
            context = c;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View converView, ViewGroup arg2) {
            HolderView holderView;
            if (converView == null) {
                holderView = new HolderView();
                converView = LayoutInflater.from(context).inflate(R.layout.search_product_item, null);
                holderView.productTitle = (TextView) converView.findViewById(R.id.search_product_item);
                converView.setTag(holderView);
            } else {
                holderView = (HolderView) converView.getTag();
            }
            String name = list.get(position).getProcutName();
            holderView.productTitle.setText(name);
            return converView;
        }

        class HolderView {
            TextView productTitle;
        }

    }
}
